import React from 'react';
import {Button,Form,Input,message} from 'antd';
import * as Utils from "../../../common/utils";
import qs from 'qs';
import * as Constant from '../../../common/const';

const FormItem = Form.Item;

class LimitVersionModal extends React.Component{
    constructor(props) {
        super(props);
    }

    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err, values) => {
            if(!err){
                this.props.saveOrEdit(qs.stringify(values)).then(res => {
                    if(res && Constant.REQUEST_SUCCESS == res.code){
                        message.success(Constant.OPERATION_SUCCESS_TIP,Constant.AUTO_DISAPPEAR_TIME);
                        this.props.cancel(true);
                    }
                });
            }
        });
    }
    validateVersion = (rule,value,callback) =>{
        const id = this.props.form.getFieldValue('id');
        this.props.validator(qs.stringify({id:id,version:value})).then(res => {
            if(res && Constant.REQUEST_SUCCESS == res.code){
                if(res.data){
                    callback();
                }else{
                    callback('验证不通过');
                }
            }else{
                callback();
            }
        });
    }
    render(){
        const {getFieldDecorator} = this.props.form;
        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 6},
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 14},
            },
        };
        const tailFormItemLayout = {
            wrapperCol: {
                xs: {
                    span: 24,
                    offset: 0,
                },
                sm: {
                    span: 16,
                    offset: 8,
                },
            },
        };

        return (
            <div>
                <Form onSubmit={this.handleSubmit}>
                    <FormItem>
                        {getFieldDecorator('id', {
                            initialValue:this.props.version && this.props.version.id,
                        })(
                            <Input type={'hidden'}/>
                        )}
                    </FormItem>
                            <FormItem label={'版本:'} {...formItemLayout}>
                                {getFieldDecorator('version', {
                                    rules: [{
                                        required: true, message: '请输入版本!',
                                    },{
                                        validator: this.validateVersion,
                                    }],
                                    initialValue:this.props.version && this.props.version.version,
                                })(
                                    <Input maxLength={255}/>
                                )}
                            </FormItem>
                    {<FormItem {...tailFormItemLayout}>
                        <Button type="primary" htmlType="submit">保存</Button>
                        <Button type="primary" onClick={this.props.cancel}>返回</Button>
                    </FormItem>}
                </Form>
            </div>
        );
    }
}

const LimitVersionModalForm = Form.create()(LimitVersionModal);
export default LimitVersionModalForm;